package com.manyiaby.view.customview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

import com.manyiaby.view.viewutils.LogUtil;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by yx on 2017/11/28.
 * 反射ScrollView，拿到它的停止属性，获取到ScrollView是否停止了
 */

public class ReflexScrollView extends ScrollView {
    public ReflexScrollView(Context context) {
        super(context);
    }

    public ReflexScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ReflexScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ReflexScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * 自定义监听,监听是否停止接口
     * 在滑动computeScroll中会不停调用
     */
    public interface OnIsFinishedListener {
        void onIsFinished(boolean isFinished);
    }

    private OnIsFinishedListener onIsFinishedListener;

    public void setOnIsFinishedListener(OnIsFinishedListener onIsFinishedListener) {
        this.onIsFinishedListener = onIsFinishedListener;
    }


    /**
     * Scrollview在滑动过程中会不断的调用onscrollChanged方法通过判断前后位置
     *
     * @param l    第一个参数为变化后的X轴位置
     * @param t    第二个参数为变化后的Y轴的位置
     * @param oldl 第三个参数为原先的X轴的位置
     * @param oldt 第四个参数为原先的Y轴的位置
     */
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
    }

    /**
     * 通过invalidate操纵，通过invalidate回去触发draw,从而computeScroll被连续调用
     */
    @Override
    public void computeScroll() {
        super.computeScroll();
        LogUtil.iYx("scrollveiwj: " + isfinishScroll());
        if (onIsFinishedListener != null) {
            onIsFinishedListener.onIsFinished(isfinishScroll());
        }
    }

    /**
     * 反射拿到ScrollView的isFinished方法判断是否停止
     * true:停止，false滑动
     *
     * @return
     */
    public boolean isfinishScroll() {
        boolean isfinish = false;
        Class scrollview = ScrollView.class;
        try {
            Field scrollField = scrollview.getDeclaredField("mScroller");//OverScroller
            scrollField.setAccessible(true);
            Object scroller = scrollField.get(this);
            Class overscroller = scrollField.getType();
            Method finishField = overscroller.getMethod("isFinished");//mScroller.fling()方法中的isFinished()：官方介绍:Returns whether the scroller has finished scrolling.
            finishField.setAccessible(true);
            isfinish = (boolean) finishField.invoke(scroller);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isfinish;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(ev);
    }
}
